package es.udc.emarketapp.emarket.model.categoryFacade.actions;

import es.udc.emarketapp.emarket.model.category.dao.SQLCategoryDAO;
import es.udc.emarketapp.emarket.model.category.dao.SQLCategoryDAOFactory;
import es.udc.emarketapp.emarket.model.category.vo.CategoryVO;
import es.udc.fbellas.j2ee.util.exceptions.InstanceNotFoundException;
import es.udc.fbellas.j2ee.util.exceptions.InternalErrorException;
import es.udc.fbellas.j2ee.util.sql.TransactionalPlainAction;
import java.sql.Connection;

public class ModifyCategoryAction implements TransactionalPlainAction {

    private Long idCat;
    private String nomCat;
    private Long catPadre;

    public ModifyCategoryAction(Long idCat, String nomCat, Long catPadre) {
        this.idCat = idCat;
        this.nomCat = nomCat;
        this.catPadre = catPadre;
    }

    public Object execute(Connection connection)
            throws InstanceNotFoundException, InternalErrorException {

        SQLCategoryDAO categoryDAO = SQLCategoryDAOFactory.getDAO();

        CategoryVO categoryVO = categoryDAO.findById(connection, idCat);
        categoryVO.setNomCat(nomCat);
        categoryVO.setCatPadre(catPadre);
        categoryDAO.update(connection, categoryVO);
        return null;
    }
}
